package com.baoy.cloud.xxx.sso.utils;

import com.baoy.cloud.xxx.common.Constant;
import com.baoy.cloud.xxx.common.utils.CookieUtil;
import com.baoy.cloud.xxx.common.utils.SpringUtils;
import com.baoy.cloud.xxx.sso.shiro.session.SerializableUtils;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.springframework.data.redis.core.RedisTemplate;

public class SsoLoginHelper {

    public static RedisTemplate redisTemplate = SpringUtils.getBean(RedisTemplate.class);

    /**
     * get sessionid by cookie (web)
     */
    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, Constant.SSO_COOKIE);
        return cookieSessionId;
    }

    /**
     * set sessionid in cookie (web)
     */
    public static void setSessionIdInCookie(HttpServletResponse response, String sessionId) {
        if (sessionId != null && sessionId.trim().length() > 0) {
            CookieUtil.set(response, Constant.SSO_COOKIE, sessionId, false);
        }
    }

    /**
     * remove sessionId in cookie (web)
     */
    public static void removeSessionIdInCookie(HttpServletRequest request,
            HttpServletResponse response) {
        CookieUtil.remove(request, response, Constant.SSO_COOKIE);
    }

    /**
     * load cookie sessionid (app)
     */
    public static String cookieSessionIdGetByHeader(HttpServletRequest request) {
        String cookieSessionId = request.getHeader(Constant.SSO_COOKIE);
        return cookieSessionId;
    }

    /**
     * login check
     */
    public static Session loginCheck(HttpServletRequest request) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId != null && cookieSessionId.trim().length() > 0) {
            return loginCheck(cookieSessionId);
        }
        return null;
    }

    /**
     * login check
     */
    public static Session loginCheck(String sessionId) {
        if (sessionId != null && sessionId.trim().length() > 0) {
            String sessionStr = (String) redisTemplate.opsForValue()
                    .get(Constant.getShiroSessionId(sessionId));
            if (StringUtils.isNotBlank(sessionStr)) {
                return SerializableUtils.deserialize(sessionStr);
            }
        }
        return null;
    }

    /**
     * client login (web)
     */
    public static void login(HttpServletResponse response, String sessionId, Session session) {
        CookieUtil.set(response, Constant.SSO_COOKIE, sessionId, false);
        redisTemplate.opsForValue()
                .set(Constant.getShiroSessionId(sessionId),
                        SerializableUtils.serialize(session),
                        session.getTimeout(), TimeUnit.MILLISECONDS);
    }

    /**
     * client login (app)
     */
    public static void login(String sessionId, Session session) {
        String sessionJson = SerializableUtils.serialize(session);
        redisTemplate.opsForValue()
                .set(Constant.getShiroSessionId(sessionId.toString()), sessionJson,
                        session.getTimeout(), TimeUnit.MILLISECONDS);
    }


    /**
     * client logout (web)
     */
    public static void logout(HttpServletRequest request, HttpServletResponse response) {
        String cookieSessionId = getSessionIdByCookie(request);
        if (cookieSessionId != null) {
            redisTemplate.delete(Constant.getShiroSessionId(cookieSessionId));
        }
        CookieUtil.remove(request, response, Constant.SSO_COOKIE);
    }

    /**
     * client logout (app)
     */
    public static void logout(String sessionId) {
        redisTemplate.delete(Constant.getShiroSessionId(sessionId));
    }

}
